package com.yinxin.JDK.DelayQueue;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.concurrent.DelayQueue;
import java.util.concurrent.TimeUnit;

/**
 * @author yinxin
 * @version 1.0
 * @Description:
 * @date 2022/7/716:29
 */
public class MyDelayDemo {
   public static void main(String[] args) throws InterruptedException {
      MyDelay myDelay1 = new MyDelay("MyDelay1", 5L, TimeUnit.SECONDS);
      MyDelay myDelay2 = new MyDelay("myDelay2", 5L, TimeUnit.SECONDS);
      MyDelay myDelay3 = new MyDelay("myDelay3", 5L, TimeUnit.SECONDS);

      DelayQueue<MyDelay> delayQueue = new DelayQueue<>();
      delayQueue.add(myDelay1);
      delayQueue.add(myDelay2);
      delayQueue.add(myDelay3);

      System.out.println("订单延迟队列开始时间:"+ LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
      while (delayQueue.size()!=0){
         System.out.println("订单延迟队列大小:"+delayQueue.size());
         MyDelay task = delayQueue.poll();
         if (task != null) {
            System.out.format("任务:{%s}被取消, 取消时间:{%s}\n", task.name, LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
         }
         Thread.sleep(1000);
      }
   }
}
